<?php
namespace App\Http\Controllers\Api;

use App\Helper\Utils\TimeUtil;
use App\V3\V3_1\Models\TallyModel;
use Illuminate\Http\Request;
use Validator;

/**
 * 记账
 * Class TallyClassController
 * @package App\Http\Controllers\Api
 */
class TallyController extends BaseController
{
    /**
     * 列表
     * change 在 2021/10/26 13:39 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
            'type' =>'required|integer',//收支类型：0-收入 1-支出
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $condition[] = ['main_id','=',$this->getMainUserId()];
        if($data['type']!=2){
            $condition[] = ['type','=',$data['type']];
        }
        //查询
        $model = TallyModel::where($condition)
            ->orderBy('id','desc')
            ->paginate($data['pageSize'],['*'], '',$data['currentPage']);
        return $this->success($model);
    }

    /**
     * 详细
     * change 在 2021/10/26 13:39 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function find(Request $request){

        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//记账分类主键id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $model = TallyModel::find($data['id']);
        return $this->success($model);
    }

    /**
     * 添加
     * change 在 2021/10/26 13:39 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'tally_class_name' =>'required|string',//记账类型名称
            'type' =>'required|integer',//收支类型：0-收入 1-支出
            'money' =>'string',//  记账金额
            'remark' =>'string',// 备注
            'proof_image' =>'string',//凭证图片(多图逗号分隔)
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $data['main_id'] = $this->getMainUserId();
        $data['created_at'] = strtotime(date("Y-m-d",time()));
        $data['add_time'] = time();
        $data['order_no'] = 'HM'.$data['main_id'].TimeUtil::getMillisecond();
        $data['proof_image'] = empty($data['proof_image'])?'':$data['proof_image'];
        TallyModel::insert($data);
        $this->log('添加记账',$data);
        return $this->success();
    }

    /**
     * 编辑
     * change 在 2021/10/26 13:40 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//记账分类主键id
            'tally_class_name' =>'string',//记账类型名称
            'type' =>'integer',//收支类型：0-收入 1-支出
            'money' =>'string',//  记账金额
            'remark' =>'string',// 备注
            'proof_image' =>'string',//凭证图片(多图逗号分隔)
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        TallyModel::where('id',$data['id'])->update($data);
        $this->log('编辑记账',$data);
        return $this->success();
    }

    /**
     * 删除
     * change 在 2021/10/26 13:40 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request){

        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required',//记账分类主键id   多条用逗号分隔
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        TallyModel::destroy(explode(',',$data['id']));
        return $this->success();
    }
}
